#include <stdio.h>
#include <omp.h>

int main(void) {
    // Ensure OMP_DISPLAY_AFFINITY is enabled in the environment:
    // set OMP_DISPLAY_AFFINITY=TRUE before running the program
    
    // API call to display affinity of primary thread
    omp_display_affinity(NULL);

    // 1st parallel region with affinity report
    #pragma omp parallel num_threads(omp_get_num_procs())
    {
        omp_display_affinity(NULL); // Display affinity in this parallel region
        if(omp_get_thread_num() == 0)
            printf("1st Parallel Region -- Affinity Reported \n");

        // doing work here
    }

    // 2nd parallel region without affinity report
    #pragma omp parallel num_threads(omp_get_num_procs())
    {
        if(omp_get_thread_num() == 0)
            printf("%s%s\n", "Same Affinity as in Previous Parallel Region", " -- no Affinity Reported\n");

        // doing more work here
    }

    // Report affinity for using half the max threads
    #pragma omp parallel num_threads(omp_get_num_procs()/2)
    {
        omp_display_affinity(NULL); // Display affinity for half the threads
        if(omp_get_thread_num() == 0)
            printf("Report Affinity for using 1/2 of max threads.\n");

        // doing work here
    }

    return 0;
}
